<?php


/**
 * initialize frontend
 *
 * @author Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright S.Ruttloff, Computer Hard- & Software
 * @version 1
 * @package core
 * @license http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 */

/**
 * initialize the frontend
 *
 * @package core
 * @version 1
 */
class login
{
    /**
     * @var object instance
     */
    private static $INSTANCE;
    /**
     * instantiate essential objects and make them avail. as subobjects
     * @param string session name (optional)
     * @return	void
     */
    private function __construct()
    {
    }
    /**
     * return a instance of object
     *
     * @param string session name (optional)
     * @return object instance
     */
    public static function getInstance()
    {
        if (!isset(self::$INSTANCE))
        {
            $c = __CLASS__;
            self::$INSTANCE = new $c ();
        }
        return self::$INSTANCE;
    }
    /**
     * check if user is logged in
     *
     * @return bool
     */
    public function checkLogin()
    {
        $frontend = frontend::getInstance();
        $sess = $frontend->getSession();
        if (!is_object($sess))
            return false;
        return ($this->getUserId() ? true : false);
    }
    /**
     * login a user
     *
     * @param int user id no
     * @param mixed optional | array of user roles or single role name
     * @return bool
     */
    public function doUserLogin($int_userid = 0, $arr_userrole = array())
    {
        if (is_null($int_userid) || !is_numeric ($int_userid))
            return false;
        if (!is_array($arr_userrole))
            $arr_userrole = array ($arr_userrole);
        $frontend = frontend::getInstance();
        $sess = $frontend->getSession();
        if (!is_object($sess))
            return false;
        $tmp = md5($user_id . serialize($user_role));
        $sess->set($tmp . '_felogin_user_id', $int_userid);
        $sess->set($tmp . '_felogin_user_role', serialize($int_userid));
        $sess->set('crc_felogin_user_crc', $tmp);
    }
    /**
     * logout a user from session
     *
     * @return  bool
     */
    public function doUserLogoff()
    {
        $frontend = frontend::getInstance();
        $sess = $frontend->getSession();
        if (!is_object($sess))
            return false;
        $tmp = $sess->set('crc_felogin_user_crc');
        $sess->delete ($tmp . '_felogin_user_id');
        $sess->delete ($tmp . '_felogin_user_role');
        $sess->delete ('crc_felogin_user_crc');
        return false;
    }
    /**
     * return user id of logged in user
     *
     * @return int user id
     */
    public function getUserId()
    {
        $frontend = frontend::getInstance();
        $sess = $frontend->getSession();
        if (!is_object($sess))
            return false;
        $crc = $sess->get ('crc_felogin_user_crc');
        return $sess->get($crc . '_felogin_user_id');
    }
    /**
     * return user role/s of logged in user
     *
     * @return array user role/s
     */
    public function getUserRole()
    {
        $frontend = frontend::getInstance();
        $sess = $frontend->getSession();
        if (!is_object($sess))
            return false;
        $crc = $sess->get ('crc_felogin_user_crc');
        return unserialize($sess->get($crc . '_felogin_user_role'));
    }
}